package com.example.algorithm_learning_platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.algorithm_learning_platform.common.Result;
import com.example.algorithm_learning_platform.entity.TeachingVideo;
import com.example.algorithm_learning_platform.service.TeachingVideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "算法教学视频管理", description = "管理算法教学视频资源")
@RestController
@RequestMapping("/api/teaching-videos")
@RequiredArgsConstructor
public class TeachingVideoController {

    private final TeachingVideoService teachingVideoService;

    @Operation(summary = "添加教学视频")
    @PostMapping
    public Result<TeachingVideo> create(@RequestBody TeachingVideo teachingVideo) {
        teachingVideoService.save(teachingVideo);
        return Result.success(teachingVideo);
    }

    @Operation(summary = "获取指定算法类型的所有视频")
    @GetMapping("/by-type/{typeId}")
    public Result<List<TeachingVideo>> listByType(
            @Parameter(description = "算法类型ID") @PathVariable Integer typeId) {
        LambdaQueryWrapper<TeachingVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachingVideo::getTypeId, typeId);
        return Result.success(teachingVideoService.list(queryWrapper));
    }

    @Operation(summary = "获取视频详情")
    @GetMapping("/{id}")
    public Result<TeachingVideo> getById(@Parameter(description = "视频ID") @PathVariable Integer id) {
        return Result.success(teachingVideoService.getById(id));
    }

    @Operation(summary = "更新视频信息")
    @PutMapping("/{id}")
    public Result<TeachingVideo> update(
            @Parameter(description = "视频ID") @PathVariable Integer id,
            @RequestBody TeachingVideo teachingVideo) {
        teachingVideo.setVideoId(id);
        teachingVideoService.updateById(teachingVideo);
        return Result.success(teachingVideo);
    }

    @Operation(summary = "删除视频")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "视频ID") @PathVariable Integer id) {
        return Result.success(teachingVideoService.removeById(id));
    }

    @Operation(summary = "分页查询视频")
    @GetMapping("/page")
    public Result<Page<TeachingVideo>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "算法类型ID") @RequestParam(required = false) Integer typeId,
            @Parameter(description = "视频标题") @RequestParam(required = false) String videoTitle) {
        
        LambdaQueryWrapper<TeachingVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(typeId != null, TeachingVideo::getTypeId, typeId)
                   .like(videoTitle != null, TeachingVideo::getVideoTitle, videoTitle);
        return Result.success(teachingVideoService.page(new Page<>(page, size), queryWrapper));
    }
} 